package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.forlove.app.qselect.login.service.ILoginPhoneService;
import com.forlove.app.qselect.login.service.impl.LoginPhoneServiceImpl;
import com.forlove.app.qselect.login.view.ILoginPhoneView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/18.
 */

public class LoginPhonePresenter extends IBasePresenter {
    private ILoginPhoneView loginPhoineView;
    private ILoginPhoneService loginPhoneService;

    public LoginPhonePresenter(ILoginPhoneView loginPhoineView) {
        this.loginPhoineView = loginPhoineView;
        loginPhoneService = new LoginPhoneServiceImpl();
        setService(loginPhoneService);
    }


    public void loadLoginPhoneData(final LoginEntity loginEntity) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", loginEntity.mobile);

        loginPhoneService.loadData(ServerManager.ACTION_LOGIN, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "success:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                String data = ParseHelper.getString(result, "data");
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(data);
                } catch (JSONException e) {

                }
                if (jsonObject != null && jsonObject.length() > 0) {
                    UserEntity userInfo = ParseHelper.getModel(result, "data", UserEntity.class);
                    userInfo.userMobile = loginEntity.mobile;
                    loginPhoineView.checkPhone(NetworkManager.ResultCode.RESULT_SUCCESS.getValue(), userInfo, "请输入密码");
                } else if (StringUtils.isNotEmpty(commonEntity.message)) {
                    loginPhoineView.checkPhone(NetworkManager.ResultCode.RESULT_SUCCESS.getValue(),
                            null, commonEntity.message);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (loginPhoineView != null) {
                        loginPhoineView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (loginPhoineView != null) {
                        loginPhoineView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
